﻿@using PagedList
@using PagedList.Mvc
@model IEnumerable<MvcShopping.Models.Product>
@{
    var ajaxOption = new AjaxOptions()
    {
        OnSuccess = "AddToCartSuccess",
        OnFailure = "AddToCartFailure",
        HttpMethod = "Post"
    };
}
@section scripts {
    @Scripts.Render("~/bundles/jqueryval")
    <script>
        function AddToCartSuccess() {
            alert('加入購物車成功');
        }
        function AddToCartFailure(xhr) {
            alert('加入購物車失敗 (HTTP 狀態碼: ' + xhr.status + ')');
        }
    </script>
}

<h2>@Html.DisplayNameFor(model => model.ToList()[0])</h2>

<h3>您正在瀏覽【@Model.First().ProductCategory.Name】分類的商品：</h3>

<table>
    <tr>
        <th>@Html.DisplayNameFor(model => model.Name)</th>
        <th>@Html.DisplayNameFor(model => model.Description)</th>
        <th>@Html.DisplayNameFor(model => model.Price)</th>
        <th>加入購物車</th>
    </tr>
@foreach (var item in Model)
{
    <tr>
        <td>@Html.ActionLink(item.Name, "ProductDetail", new { id = item.Id })</td>
        <td>@Html.DisplayFor(modelItem => item.Description)</td>
        <td>@Html.DisplayFor(modelItem => item.Price)</td>
        <td>@Ajax.ActionLink("加入購物車", "AddToCart", "Cart", new { ProductId = item.Id }, ajaxOption)</td>
    </tr>
}
</table>
<link href="~/Content/PagedList.css" rel="stylesheet" />

@{
    var data = Model as IPagedList<MvcShopping.Models.Product>;
}
@Html.PagedListPager(list: data, 
    generatePageUrl: page => Url.Action("ProductList", new { p = page }))
